function initFlowType() {
    //点击流程分类，打开设计表单
    var _this;
    var old = $('.right .r-cont').html();
    $('.flowType').on('click', function () {
        $(this).addClass("active").siblings().removeClass("active");
        _this = $(this);
        if ($(this).attr('data-id') != 0) {
            var url = $('.flowTypeUrl').val();
            url += '?id=' + $(this).attr('data-id');
            $.get(url, function (res) {
                console.log(res)
//                var html = '<p>' + _this.children().text() + '</p>';
                var html = '';
                for (var i = 0; i < res.length; i++) {
                    html += '<div class="list"><div class="l-left">';
                    html += '<i class="iconfont">&#x' + res[i].icon + '</i>&nbsp;'
                    html += res[i].flow_name;
                    html += '</div>';
                    html += '<div class="l-middle"><span class="pointer showChart" data-id="' + res[i].flow_id + '">流程设计图</span>&nbsp;<span class="pointer explain" data-id="' + res[i].flow_id + '">流程说明</span></div>';
                    html += '<div class="l-right"><a href="/index.php/Home/Flow/form/id/' + res[i].form_id + '/flow_id/' + res[i].flow_id + '">快速新建</a></div></div>';
                }
                $('.right .r-cont').html(html);
            })
        } else {
            $('.right .r-cont').html(old);
        }
    })

    //打开流程图弹窗
    $('body').on('click', '.showChart', function () {
        var flow_id = $(this).attr('data-id');
        var prcs_id = $(this).attr('data-prcs');
        var path = "/index.php/Home/Flow/design/flow_id/" + flow_id;
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '600';
        var layer_h = '500';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        if(prcs_id > 1){
            path += '/prcs_id/' + prcs_id;
        }
        layer.open({
            type: 2,
            title: [
                "流程设计图",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: path,
        })
    })

    //打开流程说明
    $('body').on('click', '.explain', function () {
        var flow_id = $(this).attr('data-id');
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '500';
        var layer_h = '400';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "流程说明",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/11/flow_id/" + flow_id,
        })
    })

    //设计流程点击展开
    $('.dr-cont li .title').on('click', function () {
        if ($(this).next().css('display') == 'none') {
            $(this).next().slideDown();
//            $(this).parent('li').siblings().find('.guide').slideUp();
            $(this).children().attr('src', '/Public/Admin/images/down.png');
        } else {
            $(this).next().slideUp();
            $(this).children().attr('src', '/Public/Admin/images/right.png');
        }
    })

    //点击设计流程分类名称，切换右边内容
    $('.flowDesign').on('click', function () {
        var cateid = $(this).parent().prev().attr('data-id');
        var name = $(this).attr('data-flowname');
        var flowId = $(this).attr('data-flowId');
        $(this).addClass("active").siblings().removeClass("active");
        switchView(name, flowId, cateid);

        //管理权限
        var url = $('.flowPrivUrl').val();
        url += '?flow_id=' + $('#flow_id').val() + '&type=1';
        $.get(url, function (pres) {
            console.log(pres)
            //预览表单功能加form_id
            $('.flowView').attr('data-formid', pres.form_id);
            console.log(pres)
            if (pres.data) {
                var html = '';
                for (var i = 0; i < pres.data.length; i++) {
                    html += '<tr><td>' + pres.data[i].priv_type + '</td>';
                    html += '<td class="auth-m">';
                    if (pres.data[i].user) {
                        html += '<p><b>用户：</b>' + pres.data[i].user + '</p>';
                    }
                    if (pres.data[i].dept) {
                        html += '<p><b>部门：</b>' + pres.data[i].dept + '</p>';
                    }
                    if (pres.data[i].role) {
                        html += '<p><b>角色：</b>' + pres.data[i].role + '</p>';
                    }
                    html += '</td>';
                    html += '<td>' + pres.data[i].priv_scope + '</td>';
                    html += '<td><a class="pointer editFlowPriv" data-id="' + pres.data[i].id + '">修改&nbsp;</a><a class="pointer delFlowPriv" data-id="' + pres.data[i].id + '">删除</a></td></tr>';
                }
                $('.power-flow .auth tbody').html(html);
            }
        })
    })

    //切换到对应的流程设计视图
    $(function () {
        var param = window.location.pathname;
        if (param.indexOf('flowDesign') >= 0) {     //flowDesign页面中执行
            var s = param.lastIndexOf('/flow_id') + 9;
            if (param.indexOf('/form_id') >= 0) {
                var e = param.lastIndexOf('/form_id');
                var fs = param.lastIndexOf('/') + 1;
                var fe = param.lastIndexOf('.');
            } else {
                var e = param.lastIndexOf('.');
            }
            var flowId = parseInt(param.substring(s, e));
            var formId = parseInt(param.substring(fs, fe));
            if (!isNaN(flowId)) {
                var url = $('#getFlowName').val() + '?flow_id=' + flowId;
                $.get(url, function (res) {
                    var name = res;
                    switchView(name, flowId, 0, formId);
                    //展开左边对应子分类
                    $('.flowDesign').each(function () {
                        if ($(this).attr('data-flowid') == flowId) {
                            $(this).parent('.cateChild').show();
                            $(this).addClass("active");
                        }
                    })
                })
            }
        }
    })

    function switchView(name, flowId, cateid, formId) {
        $('.right').hide();
        $('.designCont').show();

        if (cateid == 100) {
            $('.delete-flow').show();
            $('.guide-flow').hide();
            $('.power-flow').hide();
        } else {
            $('.delete-flow').hide();
            $('.guide-flow').show();
            $('.power-flow').show();
        }

        $('.designCont .flow-name .left').text(name)
        $('#flow_id').val(flowId);

        //设计流程的4个功能加链接
        //1.设计流程属性
        var url = $('.newFlow').attr('data-url') + '?flow_id=' + flowId;
        $('.newFlow').attr('href', url);

        //2.设计流程步骤
        var url = $('.flowStep').attr('data-url') + '?flow_id=' + flowId;
        $('.flowStep').attr('href', url);

        //3.预览表单
        $('.flowView').attr('data-formid', formId);

        //4.隐藏流程
        var url = $('.flowHide').attr('data-url');
        var end = url.indexOf('?');
        if (end >= 0) {
            url = url.substring(0, end) + '?flow_id=' + flowId;
        } else {
            url = $('.flowHide').attr('data-url') + '?flow_id=' + flowId;
        }
        $('.flowHide').attr('data-url', url);

        //显示流程
        var url = $('.flowShow').attr('data-url');
        var end = url.indexOf('?');
        if (end >= 0) {
            url = url.substring(0, end) + '?flow_id=' + flowId;
        } else {
            url = $('.flowShow').attr('data-url') + '?flow_id=' + flowId;
        }
        $('.flowShow').attr('data-url', url);

        //删除流程
        var url = $('.flowDelete').attr('data-url');
        var end = url.indexOf('?');
        if (end >= 0) {
            url = url.substring(0, end) + '?flow_id=' + flowId;
        } else {
            url = $('.flowDelete').attr('data-url') + '?flow_id=' + flowId;
        }
        $('.flowDelete').attr('data-url', url);

        //新建管理权限按钮
        $('.addFlowPriv').attr('data-id', flowId);
    }

    //设计流程点击展开
    $('.flowCate').on('click', function () {
        if ($(this).next().css('display') == 'none') {
            $(this).next().slideDown();
            $(this).parent().siblings().find('.cateChild').slideUp();
        } else {
            $(this).next().slideUp();
        }
    })

    //隐藏流程
    $('body').on('click', '.flowHide', function () {
        var url = $(this).attr('data-url');
        layer.confirm('流程隐藏后不可使用，确定要隐藏吗？', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {shade: false});
            $.get(url, function (res) {
                layer.msg(res.info, {icon: res.icon});
                location.href = res.url;
            })
        }, function (index) {
            layer.close(index);
        });
    })

    //显示流程
    $('body').on('click', '.flowShow', function () {
        var url = $(this).attr('data-url');
        $.get(url, function (res) {
            layer.msg(res.info, {icon: res.icon});
            location.href = res.url;
        })
    })

    //删除流程
    $('body').on('click', '.flowDelete', function () {
        var url = $(this).attr('data-url');
        layer.confirm('<span style="color:red;font-weight:bold">该操作将删除与之相关的所有数据，且不能恢复，确定要删除吗？</span>', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {shade: false});
            $.get(url, function (res) {
                layer.msg(res.info, {icon: res.icon});
                location.href = res.url;
            })
        }, function (index) {
            layer.close(index);
        });
    })

    //删除流程步骤
    $('.deleteStep').on('click', function () {
        var url = $(this).attr('data-url');
        layer.confirm('确定要删除该步骤吗？', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                layer.msg(res.info, {icon: res.icon});
                location.href = res.url;
            })
        }, function (index) {
            layer.close(index);
        });
    })

    //打开流程授权弹窗
    $('.addFlowPriv').on('click', function () {
        var flow_id = $(this).attr('data-id');
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '700';
        var layer_h = '800';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "新增管理权限",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/13/flow_id/" + flow_id,
        })
    })

    //修改流程授权
    $(document).on('click', '.editFlowPriv', function () {
        var flow_id = $('.addFlowPriv').attr('data-id');
        var id = $(this).attr('data-id');
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '700';
        var layer_h = '800';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "新增管理权限",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/13/flow_id/" + flow_id + "/id/" + id,
        })
    })

    //删除流程授权项
    $(document).on('click', '.delFlowPriv', function () {
        var id = $(this).attr('data-id');
        var url = $('#delFlowPrivUrl').val() + '?id=' + id;
        layer.confirm('确定要删除吗？', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {shade: false});
            $.get(url, function (res) {
                layer.msg(res.info, {'icon': res.icon});
                if (res.status) {
                    $('.power-flow .auth tbody .editFlowPriv').each(function () {
                        if ($(this).attr('data-id') == id) {
                            $(this).parents('tr').remove();
                        }
                    })
                }
                layer.close(index);
            })
        }, function (index) {
            layer.close(index);
        });
    })

    //打开预览表单弹窗
    $('.flowView').on('click', function () {
        var form_id = $(this).attr('data-formid');
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '700';
        var layer_h = '800';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "预览表单",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/14/form_id/" + form_id,
        })
    })
}

function initFormData() {
    //格式化时间
    function p(s) {
        return s < 10 ? '0' + s : s;
    }
    function getNow() {
        var date = new Date();
        return  date.getFullYear() + '-' + p(date.getMonth() + 1) + '-' + p(date.getDate()) + ' ' + p(date.getHours()) + ':' + p(date.getMinutes()) + ':' + p(date.getSeconds());
    }

    $(function () {
//        $('.form input').val('').css('width', '80%');
//        $('.form .table_bg .text_value span,font:last').text('');
//        if ($('.title').text() == '销售合同评审表' || $('.title').text() == '采购合同评审表') {
//            $('.form .table_bg .text_value font').text('');
//        }

        //去掉审批意见控件
        if ($('.text_value').last().children('span').length > 0) {
            $('.text_value').last().children('span').remove();
        }

        $('.form table').addClass('table_bg');

        $('.form .table_bg tbody tr').each(function () {
            $(this).find('td:first').addClass('text_td').addClass('text_td_1');
            $(this).find('td:last').addClass('text_value');
            var cont = $(this).find('td:last').html();
            if(cont.indexOf('{|-') !== -1 ){
                cont = cont.substring(3);
                $(this).find('td:last').html(cont);
            }
            if(cont.indexOf('-|}') !== -1 ){
                cont = cont.slice(0,-3);
                $(this).find('td:last').html(cont);
            }
        })

        //获取表单中需要自动赋值的字段
        var url = $('#FormAutoValueUrl').val();
        $.get(url, function (res) {
            console.log(res);
            //新建表单页面自动加载当前用户姓名、部门、日历等
            $('.form .table_bg .text_td input,.text_value input').each(function () {
                var key = $(this).attr('title');
                var id = $(this).attr('name');
                //去掉表单项中的隐藏选项
                if ($(this).attr('hidden') == 'hidden') {
                    $(this).removeAttr('hidden');
                }
                //去掉表单项中的控件
                if ($(this).parent('td').children('img').length > 0) {
                    $(this).parent('td').children('img').remove();
                }

                for (var i = 0; i < res.length; i++) {
                    if (key == res[i].field) {
//                        alert(key);alert(res[i].field)
                        $(this).val(res[i].value);
                        $(this).attr('readonly', true);
                    }
                }

                if (key.indexOf('时间') !== -1 || key.indexOf('日期') !== -1) {
                    $(this).addClass('datepickerInput');
                    $(this).attr('autocomplete', 'off');
                    $(this).attr('id', id);  //移动端日期插件需要用到id
                }

            })
        })

        //获取表单中不为空的字段
        var url = $('#FormNotNullFieldUrl').val();
        if (url) {
            $.get(url, function (res) {
                console.log(res)
                //新建表单页面自动加载当前用户姓名、部门、日历等
                $('.form .table_bg .text_td input,.text_value input').each(function () {
                    var key = $(this).attr('title');
                    var name = $(this).parent().prev().text();
                    name = name.replace('*', '');
                    for (var i = 0; i < res.length; i++) {
                        if (res[i].indexOf(key) >= 0) {
                            var html = '<span style="color: rgb(255, 0, 0);">* </span><span>' + name + '</span>';
                            $(this).parent().prev().html(html);
                        }
                    }
                })
                
                //textarea 加高度  add zyh 18/8/21
                $('.form .table_bg .text_value textarea').each(function(){
                    $(this).css('height','200px');
                })
            })
        }

        //新建表单页面自动加载当前用户姓名、部门、日历等
//        $('.form .table_bg .text_td input,.text_value input').each(function () {
//            var key = $(this).attr('title');
//            var id = $(this).attr('name');
//            //去掉表单项中的隐藏选项
//            if ($(this).attr('hidden') == 'hidden') {
//                $(this).removeAttr('hidden');
//            }
//            //去掉表单项中的控件
//            if ($(this).parent('td').children('img').length > 0) {
//                $(this).parent('td').children('img').remove();
//            }
//
//            if (key.indexOf('时间') != -1 || key.indexOf('日期') != -1) {
//                if (key.indexOf('申请时间') != -1 || key.indexOf('申请日期') != -1) {
//                    $(this).val(getNow());
//                } else {
//                    $(this).addClass('datepickerInput');
//                    $(this).attr('id', id);  //移动端日期插件需要用到id
//                }
//            }
//            if (key.indexOf('申请人') != -1 || key.indexOf('经办人') != -1) {
//                $(this).val($('#username').val());
//            }
//            if (key.indexOf('部门') != -1) {
//                $(this).val($('#deparment').val());
//            }
//
//        })
//        if ($('input[name=FLOW_ID]').val() != 0) {
        if ($('#flow_id').val() != 0) {
            var reg = /\{(.*?)\}/g;
            var url = $('#url').val();
            var index = layer.load(3, {shade: false});
            $.get(url, function (res) {
                console.log(res);
                var n = 0;
                for (var p1 in res.form) {//update by qiaoshoujuan 20180815 数据要和表单字段顺序对应
                    $('.form .table_bg input,select,textarea').each(function () {
                        var name = $(this).attr('name');
                        if (res.form.hasOwnProperty(p1) && (name.toLowerCase() == p1)) {
                            console.log(res.form[p1])
                            if (res.form[p1] != 'undefined') {

                                $(this).after('<span>' + res.form[p1] + '</span>');
                                $('.feedback span').text('');
                            }
                            $(this).not('.feedback textarea').remove();
                        }
                        n++;
                    })
                }
                //填充用户
                if (res.hasOwnProperty('user_ids')) {
                    var uhtml = '';
                    for (var i = 0; i < res.user_ids.length; i++) {
                        uhtml += '<span class="one-name"><a>' + res.user_names[i] + '</a>';
//                        uhtml += '<img class="pointer delOperator" data-id="'+res.user_ids[i]+'" src="/Public/Index/img/close_red.png" alt="close" width="12">';
                        uhtml += '<input type="hidden" name="user_ids[]" value="' + res.user_ids[i] + '"><input type="hidden" name="user_names[]" value="' + res.user_names[i] + '"></span>';
                    }
                    $('.text_value .auto_user').html(uhtml);
                } else {
                    $('.text_value .auto_user').html('<span>无</span>');
                }
                //会签意见 
                if (res.feed[n] != 'undefined') {
                    var html = '';
                    for (var i = 0; i < res.feed.length; i++) {
                        html += '<div style="margin-left:20px"><p>第' + res.feed[i].prcs_id + '步：' + res.feed[i].prcs_name + '&nbsp;&nbsp;' + res.feed[i].user_name + '<br/>' + res.feed[i].content + '&nbsp;&nbsp;' + res.feed[i].edit_time + '</p></div>';
                    }
                    var len = $('.form .table_bg .text_td').length;
                    var num = 0;
                    $('.form .table_bg .text_td').each(function () {
                        num += 1;
                        if ($(this).text().indexOf('意见') != -1  || (num == len)) {
                            $(this).next().html(html);
                        }
                    })
                }
                layer.close(index);
            })
//        } else if ($('input[name=FLOW_ID]').val() == 0 && ($('input[name=isEdit]').val() == 'edit')) {
        } else if ($('#flow_id').val() == 0 && ($('input[name=isEdit]').val() == 'edit')) {
            var url = $('#url').val();
            var index = layer.load(3, {shade: false});
            $.get(url, function (res) {
                console.log(res)
                var n = 0;
                for (var p1 in res.form) {//update by qiaoshoujuan 20180815 数据要和表单字段顺序对应
                    $('.form .table_bg input,select,textarea').each(function () {
                        var name = $(this).attr('name');
                        if (res.form.hasOwnProperty(p1) && (name.toLowerCase() == p1)) {
                            console.log(res.form[p1])
                            if (res.form[p1] != 'undefined' && ($(this).is('select') == false)) {
                                $(this).val(res.form[p1])
                                $(this).text(res.form[p1]);
                            }
                        }
                        n++;
                    })
                }

                //填充用户
                if (res.hasOwnProperty('user_ids')) {
                    var uhtml = '';
                    for (var i = 0; i < res.user_ids.length; i++) {
                        uhtml += '<span class="one-name"><a>' + res.user_names[i] + '</a>';
                        uhtml += '<img class="pointer delOperator" data-id="' + res.user_ids[i] + '" src="/Public/Index/img/close_red.png" alt="close" width="12">';
                        uhtml += '<input type="hidden" name="user_ids[]" value="' + res.user_ids[i] + '"><input type="hidden" name="user_names[]" value="' + res.user_names[i] + '"></span>';
                    }
                    $('.text_value .auto_user img:last').after(uhtml);
                } else {
                    $('.text_value .auto_user').html('<span>无</span>');
                }
                $('input[name=FLOW_ID]').val(res.flow_id);
                layer.close(index);
            })
        }
    })

    function isContains(str, substr) {
        // alert(str);
        //  alert(substr)
        return str.indexOf(substr);
    }

    //回退
    $('.back').on('click', function () {
        var flow_id = $('input[name=flow_id]').val();
        var run_id = $('input[name=run_id]').val();
        var prcs_id = $('input[name=prcs_id]').val();
        var currPrcs = $('input[name=currPrcs]').val(); //当前流程设计号
        var prcsNum = $('input[name="prcsNum"]').val(); //当前流程标记号
        var feedback = $('textarea[name="feedback"]').val(); //回退时获取审批意见 
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '550';
        var layer_h = '550';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "选择回退步骤",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/2/flow_id/" + flow_id + "/run_id/" + run_id + "/prcs_id/" + prcs_id + "/currPrcs/" + currPrcs+ "/prcsNum/"+prcsNum+"/feedback/"+feedback,
        })
    })

    //打开选择用户弹窗
    $('body').on('click', '.checkUser', function () {
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '550';
        var layer_h = '550';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "选择人员",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/1/form/1/one/0"
        })
    })

    //删除主办人或经办人
    $('body').on('click', '.delOperator', function () {
        $(this).parent('span').remove();
    })
}

function adminFlowStep() {
    $('.checkDept').on('click', function () {
        var txt = $(this).attr('data-name');
        var prcs_id = $(this).attr('data-id');
        //var url = $('input[name=prcs_type]').attr('data-url');
        var url = $(this).find('input[name=prcs_type]').attr('data-url');
        if ($(this).children('img').attr('src') == '/Public/Index/img/s2.png') {
            $(this).children('img').attr('src', '/Public/Index/img/s1.png');
            $(this).siblings().children('img').attr('src', '/Public/Index/img/s2.png');
            $(this).children('input').val(1);
            $(this).siblings().children('input').val(0);

            $('#prcs_id').val(prcs_id);
        }
        if (url) {
            getData(txt, prcs_id, url);
        }

    })

    $(function () {
        var txt = $('#prcs_name_html').val();
        var prcs_id = $('#prcs_id_html').val();
        var url = $('#prcs_url_html').val();
        if (url) {
            getData(txt, prcs_id, url)
        }

    })

    function getData(txt, id, url) {
//         alert(txt);alert(id);alert(url)
        $('.stepName').text(txt);
        $('#prcs_id').val(id);
        var index = layer.load(2, {shade: false});
        $.get(url, function (res) {
            console.log(res);
            var len = res.length;
            var html = '';
            if (len == 1) {
                html += '<p class="people main_user"><span class="one">主办人：</span><span><a>' + res[0].user_name + '</a><img class="pointer delOperator" data-id="' + res[0].user_id + '" src="/Public/Index/img/close_red.png" alt="close" width="15" height="15"/>';
                html += '<input type="hidden" name="user_id" value="' + res[0].user_id + '"><input type="hidden" name="user_name" value="' + res[0].user_name + '"></span></p>';
                html += '<p class="other_user"><span class="one">经办人：</span><span><a>' + res[0].user_name + '</a><img class="pointer delOperator" data-id="' + res[0].user_id + '" src="/Public/Index/img/close_red.png" alt="close" width="15" height="15"/>';
                html += '<input type="hidden" name="user_ids[]" value="' + res[0].user_id + '"><input type="hidden" name="user_names[]" value="' + res[0].user_name + '"></span></p>';
            } else if (len > 1 && res[0].top_default == 2 && res[0].auto_type != 0) {
                html += '<p class="people  main_user"><sp= 2 &&an class="one">无主办人</span></p>';
                html += '<p class="other_user"><span class="one">经办人：</span>';
                for (var i = 0; i < res.length; i++) {
                    html += '<span style="margin-right:10px"><a>' + res[i].user_name + '</a><img class="pointer delOperator" data-id="' + res[i].user_id + '" src="/Public/Index/img/close_red.png" alt="close" width="15" height="15" />';
                    html += '<input type="hidden" name="user_ids[]" value="' + res[i].user_id + '"><input type="hidden" name="user_names[]" value="' + res[i].user_name + '"></span>';
                }
                html += '</p>';
            } else if (len > 1 && res[0].top_default == 2 && res[0].auto_type == 0) {
                html += '<p class="people  main_user"><sp= 2 &&an class="one">无主办人</span></p>';
                html += '<p class="other_user"><span class="one">经办人：</span></p>';
            } else if (len > 1 && res[0].top_default == 0 && res[0].auto_type == 0) {
                html += '<p class="people main_user"><span class="one">主办人：</span></p>';
                html += '<p class="other_user"><span class="one">经办人：</span></p>';
            }

            if (html) {
                $('.operator').html(html);
            } else {
                html = '<p class="people main_user"><span class="one">主办人：</span></p><p class="other_user"><span class="one">经办人：</span></p>';
                $('.operator').html(html);
            }
            layer.close(index);
        })
    }

    //打开部门弹窗
    $('.checkUser').on('click', function () {
        var prcs_id = $('#prcs_id').val();
        var flow_id = $('#flow_id').val();
        var run_id = $('#run_id').val();
        var auto_type = $('#auto_type').val();
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '550';
        var layer_h = '350';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "选择人员",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/1/flow_id/" + flow_id + "/run_id/" + run_id + "/prcs_id/" + prcs_id + "/auto_type/" + auto_type
        })
    })

    //删除主办人或经办人
    $('body').on('click', '.delOperator', function () {
        $(this).parent('span').remove();
    })

    //清空主办人和经办人
    $('.clear-user').on('click', function () {
        $('.main_user span:not(:first)').remove();
        $('.other_user span:not(:first)').remove();
    })

    //转交一下步
    $('.sure').on('click', function () {
        if ($('.operator .main_user .one').text() != '无主办人') {
            if ($('.operator .main_user span:last a').text() == '') {
                layer.msg('主办人不能为空');
                return false;
            }

            if ($('.operator .other_user span:last a').text() == '') {
                layer.msg('经办人不能为空');
                return false;
            }
        }
        $(this).addClass('btn-submit');
        $('.btn-submit').click();

    })

    //结束流程
    $('.endFlow').on('click', function () {
        var data = $('form').serialize();
        var url = $(this).attr('data-url');
        layer.confirm('确定要结束该流程吗', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {shade: false});
            $.post(url, data, function (res) {
                console.log(res);
                layer.msg(res.info);
                location.href = res.url;
            })
        }, function (index) {
            layer.close(index);
        });

    })
}

//列表js
function initFlowList() {
    $('.remind').on('click', function () {
        var _this = $(this);
        var url = $(this).attr('data-url');
        $.get(url, function (res) {
            console.log(res);
            if (res.status == 1) {
                layer.msg(res.msg);
                var title = '上次催办时间：' + res.remind;
                _this.attr('title', title);
                _this.children('img').attr('src', '/Public/Admin/images/flow_remind.png');
            }
        })
    })

    //导出
    $('.download').on('click', function () {
        var url = $(this).attr('data-url');
        var name = $(this).attr('data-name');
        url += '/download/' + name;
        $(this).attr('href', url).attr('target', '_black');
        window.open(url);
    })

    $('.delForm').on('click', function () {
        var url = $(this).attr('data-url');
        layer.confirm('确定要删除该表单吗?', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.get(url, function (res) {
                console.log(res);
                layer.msg(res.info, {icon: res.icon});
                if (res.status == 1) {
                    location.href = res.url;
                }
            })
        })
    })

    //按人员查询js
    $('select[name=range]').on('change', function () {
        if ($(this).val() > 0) {
            $('#user').show();
        } else {
            $('#user').hide();
        }
    })

    $(function () {
        if ($('select[name=range]').find('option:selected').val() != 0) {
            $('#user').show();
        } else {
            $('#user').hide();
        }
    })

    //打开选择人员弹窗
    $('.flowCheckUser').on('click', function () {
        var one = $('#one').val();
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '550';
        var layer_h = '550';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "选择人员",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/1/one/" + one
        })
    })

    //清空指定人员和角色
    $('.clearData').on('click', function () {
        $(this).parent().find('div').html('');
    })

    //重走流程
    $('.repeatFlow').on('click', function () {
        var url = $(this).attr('data-url');
        layer.confirm('确定从第一步开始重新执行？', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {shade: false});
            $.get(url, function (res) {
                layer.msg(res.info, {icon: res.icon});
                if (res.status == 1) {
                    location.reload();
                }
            })
        }, function (index) {
            layer.close(index);
        });
    })

    //强制结束流程
    $('.endFlow').on('click', function () {
        var url = $(this).attr('data-url');
        layer.confirm('确定要强制结束该流程？', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {shade: false});
            $.get(url, function (res) {
                layer.msg(res.info, {icon: res.icon});
                if (res.status == 1) {
                    location.reload();
                }
            })
        }, function (index) {
            layer.close(index);
        });
    })

    //强制删除流程
    $('.delFlow').on('click', function () {
        var url = $(this).attr('data-url');
        layer.confirm('确定要强制删除该流程吗？删除后无法恢复！', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {shade: false});
            $.get(url, function (res) {
                layer.msg(res.info, {icon: res.icon});
                if (res.status == 1) {
                    location.reload();
                }
            })
        }, function (index) {
            layer.close(index);
        });
    })

}

function initFlowStep() {
    $(function () {
        $('body').on('click', '.flow-step', function () {
            if ($(this).hasClass('check')) {
                $(this).removeClass('check');
                $(this).children('input').val('');
            } else {
                $(this).addClass('check');
                $(this).children('input').val($(this).attr('data-step'));
            }
        })

        //编辑时自动展开所有
        if ($('input[name=prcs_id]').val() != 0) {
            $('#select-role').show();
            $('#select-role').prev().find('img').attr('src', '/Public/Admin/images/down.png')
            $('#other-set').show();
            $('#other-set').prev().find('img').attr('src', '/Public/Admin/images/down.png')
        }

        //判断newFlowStep页面中，经办人或角色框有值，默认展开及展开上级div
        var userlen = $('.value .auto_user span').length;
        if (userlen > 1) {
            $('#auto_user').show();
        }
        var privlen = $('.value .priv span').length;
//        alert(privlen)
        if (privlen > 1) {
            $('#auto_priv').show();
        }
    })

    $('.a-left').on('click', function () {
        var html = '';
        $('.flow-step').each(function () {
            if ($(this).hasClass('check')) {
                html += '<li class="pointer add-step" data-step="' + $(this).attr('data-step') + '">' + $(this).html() + '</li>';
                $(this).remove();
            }
        })

        if ($('.left ul li').text()) {
            $('.left ul li:last').after(html);
        } else {
            $('.left ul').html(html)
        }
    })

    $('body').on('click', '.add-step', function () {
        if ($(this).hasClass('check')) {
            $(this).removeClass('check');
            $(this).children('input').val('');
        } else {
            $(this).addClass('check');
            $(this).children('input').val('');
        }
    })

    $('.a-right').on('click', function () {
//        var html = '';
        $('body').find('.add-step').each(function () {
            if ($(this).hasClass('check')) {
                var html = '<li class="pointer flow-step" data-step="' + $(this).attr('data-step') + '">' + $(this).html() + '</li>';
                $(this).remove();

                var step = $(this).attr('data-step');
                if ($('.right ul li').text()) {
                    $('body').find('.flow-step').each(function () {
                        if (step == 'end') {
                            $('.right ul li:last').after(html);
                            return false;
                        }
                        if ($(this).attr('data-step') > step) {
                            $(this).before(html);
                            return false;
                        }
                        if ($(this).attr('data-step') == 0) {
                            $(this).before(html);
                        }
                    })
                } else {
                    $('.right ul').html(html)
                }
            }
        })
    })

    //点击展开，收起
    $('.arrow').on('click', function () {
        if ($(this).parent().parent().next().css('display') == 'none') {
            $(this).parent().parent().next().slideDown();
            $(this).attr('src', '/Public/Admin/images/down.png');
        } else {
            $(this).parent().parent().next().slideUp();
            $(this).attr('src', '/Public/Admin/images/up.png');
        }
    })

    //打开选择人员弹窗
    $('.flowCheckUser').on('click', function () {
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '550';
        var layer_h = '550';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        var flow_id = $('input[name="flow_id"]').val();
        layer.open({
            type: 2,
            title: [
                "选择人员",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/1/flow_type/1/flow_id/" + flow_id
        })
    })

    //选择部门
    $(document).on('click', '.addDept', function () {
        var field = $('#deptField').val();  //数据表中的键名
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '550';
        var layer_h = '550';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "选择部门",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/3/field/" + field
        })
    })

    //选人规则中不同选项的页面切换
    $('select[name=auto_type]').on('change', function () {
        if ($(this).val() == 3) {
            //展开选择默认人员选项
            $('#auto_user').slideDown();
        } else {
            $('#auto_user').hide();
        }
        if ($(this).val() == 9) {
            //展开选择默认角色选项
            $('#auto_priv').slideDown();
        } else {
            $('#auto_priv').hide();
        }
    })

    //选择角色
    $('.addPriv').on('click', function () {
        var field = $('#field').val();  //数据表中的键名
        var document_w = $(window).width();
        var document_h = $(window).height();
        var layer_w = '300';
        var layer_h = '500';
        var w = (document_w - layer_w) / 2;
        var h = (document_h - layer_h) / 2;
        layer.open({
            type: 2,
            title: [
                "选择角色",
                'background-color: #437dba;color:#fff;text-align:center;font-size:1.4em',
            ],
            area: [layer_w + 'px', layer_h + 'px'], //宽高
            offset: [h + 'px', w + 'px'], //弹出位置 [top,left]
            content: "/index.php/Home/Index/layer/type/7/field/" + field
        })
    })

    //清空指定人员和角色
    $('.clearData').on('click', function () {
        $(this).parent('.value').find('.textarea span:not(:first)').remove(); //经办人和角色
        $(this).parents('#auto_user').find('.auto_user_op').html('');  //主办

    })

    //删除单个用户
    $('body').on('click', '.delOperator', function () {
        $(this).parent('span').remove();
    })

}

//设计流程条件中的js
function initFlowCondition() {
    $('.editPrcsIn').on('click', function () {
        $(this).parent().prev().find('span').addClass('hide');
        $(this).parent().prev().find('input').removeClass('hide');
    })

    $(document).on('click', function (e) {
        console.log(e);
        if (e.target.className != "editPrcsIn pointer" && e.target.className != "layui-input conInput") {
            $('.prcsStr').each(function () {
                var _this = $(this);
                if ($(this).children('span').hasClass('hide')) {
                    var con = $(this).children('input').val();
                    $(this).children('span').text(con).removeClass('hide');
                    $(this).children('input').val(con).addClass('hide');
                    var url = $('#flowConditionUrl').val();
                    var data = $('form').serialize();
                    $.post(url, data)
                }
            })
        }
    })

    //增加转入条件
    $('.addPrcsIn').on('click', function () {
        if ($('.value').val()) {
            var value = "'" + $('.value').val() + "'";
        } else {
            var value = "''";
        }
        var str = "'" + $('.field').val() + "'" + $('.sign').val() + value;
        var len = $('.prcsIn tr').length;
        var html = '<tr><td>' + len + '</td>';
        html += '<td><span>' + str + '</span><input type="text" class="layui-input conInput hide" name="prcs_in[]" value="' + str + '" /></td>';
        html += '<td><a class="editPrcsIn pointer">编辑&nbsp;</a><a class="pointer delPrcsIn">删除</a></td>';
        $('.prcsIn tr:last').after(html)
    })

    //增加转出条件
    $('.addPrcsOut').on('click', function () {
        if ($('.value').val()) {
            var value = "'" + $('.value').val() + "'";
        } else {
            var value = "''";
        }
        var str = "'" + $('.field').val() + "'" + $('.sign').val() + value;
        var len = $('.prcsOut tr').length;
        var html = '<tr><td>' + len + '</td>';
        html += '<td><span>' + str + '</span><input type="text" class="layui-input conInput hide" name="prcs_out[]" value="' + str + '" /></td>';
        html += '<td><a class="editPrcsOut pointer">编辑&nbsp;</a><a class="pointer delPrcsIn">删除</a></td>';
        $('.prcsOut tr:last').after(html)
    })

    //删除当前条件
    $(document).on('click', '.delPrcsCon', function () {
        var _this = $(this);
        layer.confirm('确定要删除此条件吗？', {
            closeBtn: 0,
            btn: ['确定', '取消'] //按钮
        }, function () {
//            var index = layer.load(2, {shade: false});
            _this.parents('tr').remove();
            layer.closeAll();
        });
    })


}








